package com.example.myapplication.ui;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.myapplication.R;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class DataAdapters extends RecyclerView.Adapter<DataAdapters.DataHolder> {

    private List<String> data = null;

    public DataAdapters(List<String> data) {
        if (data != null) {
            this.data = data;
        }else {
            this.data = new ArrayList<>();
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    public void setData(String data){
        if (data!= null){
            this.data.add(data);
            notifyDataSetChanged();
        }
    }

    @NonNull
    @Override

    public DataHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_data, null);
        return new DataHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull DataHolder holder, int position) {
        holder.tv.setText(data.get(position));
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    static class DataHolder extends RecyclerView.ViewHolder {

        private TextView tv;

        public DataHolder(@NonNull View itemView) {
            super(itemView);
            tv = itemView.findViewById(R.id.data);
        }
    }
}
